/*
 * MidterBotcontrollerPC.java
 *
 * Created on __DATE__, __TIME__
 */

import java.io.DataOutputStream;
import java.io.IOException;

import lejos.pc.comm.*;

/**
 *
 * @author  __USER__
 */
public class MidterBotcontrollerPC extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DataOutputStream dos;

	/** Creates new form MidterBotcontrollerPC */
	public MidterBotcontrollerPC() {
		initComponents();

		StatusText.setText("Starting up");

		NXTConnector conn = new NXTConnector();

		StatusText.setText("Connecting");

		boolean connected = conn.connectTo("btspp://Gert4");

		if (!connected) {
			System.err.println("Failed to connect to any NXT");
			System.exit(1);
		}

		StatusText.setText("Connected");

		dos = conn.getDataOut();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		StatusLabel = new javax.swing.JLabel();
		StatusText = new javax.swing.JTextField();
		LeftMotorForwardButton = new javax.swing.JButton();
		LeftMotorStopButton = new javax.swing.JButton();
		LeftMotorBackwardButton = new javax.swing.JButton();
		PowerUpButton = new javax.swing.JButton();
		PowerDownButton = new javax.swing.JButton();
		RightMotorForwardButton = new javax.swing.JButton();
		RightMotorStopButton = new javax.swing.JButton();
		RightMotorBackward = new javax.swing.JButton();
		DisConnectButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		StatusLabel.setText("Status");

		StatusText.setEditable(false);

		LeftMotorForwardButton.setText("Forward");
		LeftMotorForwardButton
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						LeftMotorForwardButtonActionPerformed(evt);
					}
				});

		LeftMotorStopButton.setText("Stop");
		LeftMotorStopButton
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						LeftMotorStopButtonActionPerformed(evt);
					}
				});

		LeftMotorBackwardButton.setText("Bacward");
		LeftMotorBackwardButton
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						LeftMotorBackwardButtonActionPerformed(evt);
					}
				});

		PowerUpButton.setText("More Power");
		PowerUpButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				PowerUpButtonActionPerformed(evt);
			}
		});

		PowerDownButton.setText("Less Power");
		PowerDownButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				PowerDownButtonActionPerformed(evt);
			}
		});

		RightMotorForwardButton.setText("Forward");
		RightMotorForwardButton
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						RightMotorForwardButtonActionPerformed(evt);
					}
				});

		RightMotorStopButton.setText("Stop");
		RightMotorStopButton
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						RightMotorStopButtonActionPerformed(evt);
					}
				});

		RightMotorBackward.setText("Bacward");
		RightMotorBackward
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						RightMotorBackwardActionPerformed(evt);
					}
				});

		DisConnectButton.setText("DisConnect");
		DisConnectButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				DisConnectButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		StatusLabel,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		58,
																		Short.MAX_VALUE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		StatusText,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		131,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addGap(83, 83,
																		83))
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								false)
																								.addComponent(
																										LeftMotorStopButton,
																										javax.swing.GroupLayout.Alignment.LEADING,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										Short.MAX_VALUE)
																								.addComponent(
																										LeftMotorForwardButton,
																										javax.swing.GroupLayout.Alignment.LEADING,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										Short.MAX_VALUE))
																				.addComponent(
																						LeftMotorBackwardButton))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																				.addComponent(
																						DisConnectButton,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						PowerDownButton,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						PowerUpButton,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																				.addComponent(
																						RightMotorStopButton,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						RightMotorForwardButton,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						RightMotorBackward,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE))))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(StatusLabel)
												.addComponent(
														StatusText,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		LeftMotorForwardButton)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.BASELINE)
																				.addComponent(
																						LeftMotorStopButton)
																				.addComponent(
																						DisConnectButton))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		LeftMotorBackwardButton))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		PowerUpButton)
																.addGap(39, 39,
																		39)
																.addComponent(
																		PowerDownButton))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		RightMotorForwardButton)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		RightMotorStopButton)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		RightMotorBackward)))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
		//GEN-END:initComponents

	private void DisConnectButtonActionPerformed(
			java.awt.event.ActionEvent evt) {
		sendMessage(99);
	}

	private void RightMotorForwardButtonActionPerformed(
			java.awt.event.ActionEvent evt) {
		sendMessage(5);
	}

	private void RightMotorStopButtonActionPerformed(
			java.awt.event.ActionEvent evt) {
		sendMessage(7);
	}

	private void RightMotorBackwardActionPerformed(
			java.awt.event.ActionEvent evt) {
		sendMessage(6);
	}

	private void PowerDownButtonActionPerformed(java.awt.event.ActionEvent evt) {
		sendMessage(10);
	}

	private void PowerUpButtonActionPerformed(java.awt.event.ActionEvent evt) {
		sendMessage(9);
	}

	private void LeftMotorForwardButtonActionPerformed(
			java.awt.event.ActionEvent evt) {
		sendMessage(3);
	}

	private void LeftMotorStopButtonActionPerformed(
			java.awt.event.ActionEvent evt) {
		sendMessage(8);
	}

	private void LeftMotorBackwardButtonActionPerformed(
			java.awt.event.ActionEvent evt) {
		sendMessage(4);
	}

	private void sendMessage(int message) {
		try {
			dos.writeByte(message);
			dos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args the command line arguments
	 * @throws NXTCommException 
	 */
	public static void main(String args[]) throws NXTCommException {

		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new MidterBotcontrollerPC().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton DisConnectButton;
	private javax.swing.JButton LeftMotorBackwardButton;
	private javax.swing.JButton LeftMotorForwardButton;
	private javax.swing.JButton LeftMotorStopButton;
	private javax.swing.JButton PowerDownButton;
	private javax.swing.JButton PowerUpButton;
	private javax.swing.JButton RightMotorBackward;
	private javax.swing.JButton RightMotorForwardButton;
	private javax.swing.JButton RightMotorStopButton;
	private javax.swing.JLabel StatusLabel;
	private javax.swing.JTextField StatusText;
	// End of variables declaration//GEN-END:variables

}